package com.zhangjianling.premp.controller.sys;

import com.zhangjianling.premp.controller.base.BaseController;
import com.zhangjianling.premp.entity.sys.ResultEntity;
import com.zhangjianling.premp.entity.sys.SysLog;
import com.zhangjianling.premp.server.sys.SysLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.net.SocketException;

/**
 * 系统日志管理
 * @author zhangjl
 *
 */
@RestController
@RequestMapping("sysLog")
public class SysLogController extends BaseController {
	
	@Resource
	private SysLogService sysLogService;
	/**
	 * 分页用户列表
	 * 
	 * @return
	 */
	@RequiresPermissions("sysLog:pageList")
	@PostMapping("pageList")
	public ResultEntity pageList(@RequestParam(value = "pageCurrent", defaultValue = "1") String pageCurrent, SysLog log) {
		ResultEntity res = new ResultEntity();
		try {
			Page<SysLog> userPage = sysLogService.pageList(Integer.parseInt(pageCurrent), PAGESIZE, "createDate",log);
			if (userPage.getTotalPages() > 0) {
				res.setData(userPage);
				res.setMessage(GET_SUCCESS);
				res.setCode(SUCCESS);
			} else {
				res.setMessage(IS_NULL);
				res.setCode(NULL);
			}
		} catch(SocketException e){
			e.printStackTrace();
			res.setMessage("网络异常:"+e.getMessage());
			res.setCode(ERROR);
		}catch (Exception e) {
			e.printStackTrace();
			res.setMessage(GET_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}

	/**
	 * 查询单个用户
	 * @return
	 */
	@GetMapping("findLog")
	public ResultEntity findLog(String ID){
		ResultEntity res = new ResultEntity();
		try {
			res.setData(sysLogService.findLog(ID));
			res.setMessage(GET_SUCCESS);
			res.setCode(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(GET_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}
	
}
